package com.carltechnology.mahjonggamecaculation.dao;

import com.carltechnology.mahjonggamecaculation.base.MyAbstractSubscriber;
import com.carltechnology.mahjonggamecaculation.dao.impl.MahjongGameModifyComponent;
import com.carltechnology.mahjonggamecaculation.mode.IGameData;
import com.carltechnology.mahjonggamecaculation.mode.IGameRuntimeData;
import com.carltechnology.mahjonggamecaculation.utils.Utils;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by WZTCM on 2016/6/28.
 */
public class MahjongModifyHelper {
    private IMahjongGameModifyComponent iModifyComponent;
    public MahjongModifyHelper(IMahjongDataDao dao) {
        iModifyComponent = new MahjongGameModifyComponent(dao);
    }
    public void modifyGameData(MyAbstractSubscriber<IGameData> subscriber, final IGameData mode){
        getGameDataModifyObservable(mode).subscribe(subscriber);
    }

    public void modifyRuntimeGameData(MyAbstractSubscriber<IGameRuntimeData> subscriber,final IGameRuntimeData mode){
        getRuntimeGameModifyObservable(mode).subscribe(subscriber);
    }
    public void clearRuntimeGameData(MyAbstractSubscriber<Boolean> subscriber){
        getClearRuntimeGameObservable().subscribe(subscriber);
    }


    private Observable<IGameData> getGameDataModifyObservable(
                                                    final IGameData gameData){
        return Observable.create(new Observable.OnSubscribe<IGameData>() {
            @Override
            public void call(Subscriber<? super IGameData> subscriber) {
                iModifyComponent.modifyMahjongDataDate(subscriber,gameData);
            }
        }).onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SMALL_SIZE.getConstant())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


    private Observable<IGameRuntimeData> getRuntimeGameModifyObservable(final IGameRuntimeData mode){
        return Observable.create(new Observable.OnSubscribe<IGameRuntimeData>() {
            @Override
            public void call(Subscriber<? super IGameRuntimeData> subscriber) {
                iModifyComponent.modifyMahjongRuntimeData(subscriber,mode);
            }
        }).onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SMALL_SIZE.getConstant())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    private Observable<Boolean> getClearRuntimeGameObservable(){
        return Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                iModifyComponent.clearRuntimeData(subscriber);
            }
        });
    }


}
